from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from panda3d.core import ConfigVariableString
from panda3d.core import loadPrcFileData
from server.networking.networkmanager import NetworkManager
from server.sessionstuff.sessionmanager import SessionManager
from server.gui.guimanager import GuiManager


class GameServer(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        self.setFrameRateMeter(True)

        ConfigVariableString('text-quality-level', 'fastest')

        self.networkmanager = NetworkManager()
        self.sessionmanager = SessionManager()
        self.guimanager = GuiManager()

        self.exitFunc = self.exit_func
        self.gameTask = self.taskMgr.add(self.game_loop, "game_loop")

    def game_loop(self, task):
        self.networkmanager.update(globalClock.getDt())
        self.sessionmanager.update(globalClock.getDt())
        self.guimanager.update(globalClock.getDt())
        # Since every return is Task.cont, the task will loop infinitely
        return Task.cont

    def exit_func(self):
        self.networkmanager.exit()

server = GameServer()
server.run()